package org.apache.maven.wagon.providers.http;

import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferEventSupport;
import org.apache.maven.wagon.resource.Resource;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA. User: ray Date: Feb 28, 2008 Time: 4:08:43 PM To
 * change this template use File | Settings | File Templates.
 */
public class PostInputStream
    extends FileInputStream

{

  private TransferEventSupport eventSupport;

  private TransferEvent event;

  public PostInputStream(File file, Resource resource, Wagon wagon,
      TransferEventSupport eventSupport) throws FileNotFoundException {
    super(file);

    this.eventSupport = eventSupport;

    event = new TransferEvent(wagon, resource, TransferEvent.TRANSFER_PROGRESS,
        TransferEvent.REQUEST_PUT);

    event.setLocalFile(file);
  }

  public int read(byte b[]) throws IOException {
    return read(b, 0, b.length);
  }

  public int read() throws IOException {
    byte b[] = new byte[1];

    return read(b);
  }

  public int read(byte b[], int off, int len) throws IOException {
    int retValue = super.read(b, off, len);

    if (retValue > 0) {
      event.setTimestamp(System.currentTimeMillis());

      eventSupport.fireTransferProgress(event, b, retValue);
    }
    return retValue;
  }
}

